package com.asset.dispoal.business.bsEvidenceRecognition.service.commom;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.asset.dispoal.business.casedispose.domain.db.mapper.CasedisposeMapper;
import com.asset.dispoal.business.match.dto.MatchRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.redis.commands.IRedisExtCommands;

import java.util.Set;

@Component
@Slf4j
public class EvidenceCaseMatchResultCommand implements BaseCommand<MatchRequest, Result> {

    @Autowired
    private IRedisExtCommands redisExtCommands;

    @Autowired
    private CasedisposeMapper casedisposeMapper;

    @Autowired
    BaseIdeable<String> snowFlak;

    @Override
    public Result execute(MatchRequest matchRequest) {
        String requestKey = matchRequest.getData().getRequestKey();
        if (matchRequest.isFaild()) {
            redisExtCommands.hdel(6, "ocr_case_data", requestKey);
            redisExtCommands.hdel(6, "ocr_case_before", requestKey);
            return Result.ok();
        }
        String responseKey = matchRequest.getData().getResponseKey();
        String notifyKey = responseKey + "_" + requestKey;
        String matchResult = redisExtCommands.hget(6, "notify_case_key", notifyKey);
        String matchData = redisExtCommands.hget(6, "ocr_case_data", requestKey);
        JSONArray jsonDataArray = JSON.parseArray(matchData);
        JSONObject jsonObj = JSON.parseObject(matchResult);
        Set<String> evidenceIdSet = jsonObj.keySet();
        for (String evidenceId : evidenceIdSet) {
            JSONObject evidenceMatchJson = jsonObj.getJSONObject(evidenceId);
            evidenceMatchJson.remove("evidenceItemId");
            Set<String> params = evidenceMatchJson.keySet();
            if (CollectionUtil.isNotEmpty(params)) {
                for (String param : params) {
                    JSONArray matchResultArr = evidenceMatchJson.getJSONArray(param);
                    for (Object matchResultObj : matchResultArr) {
                        String genMapperId = snowFlak.generateId();
                        casedisposeMapper.insertResult(genMapperId, evidenceId, param, matchResultObj);
                        String id = jsonDataArray.stream().filter(data -> matchResultObj.equals(((JSONObject) data).getString(param))).map(data -> ((JSONObject) data).getString("id")).findFirst().get();
                        casedisposeMapper.updateResultId(evidenceId, id);
                    }
                }
            }
        }
        redisExtCommands.hdel(6, "ocr_case_data", requestKey);
        redisExtCommands.hdel(6, "ocr_case_before", requestKey);
        redisExtCommands.hdel(6, "notify_case_key", notifyKey);
        return Result.ok();
    }
}
